/**
 * 按父节点删除BOM结构表中的结构关系
 * 
 * 主要用于保证同一大版本的BOM结构，中间表只保留一条记录
 * 
 * @author 魏文杰 
 * @Version 1.0
 * @Date 2013-10-08
 */

package ext.generic.erp.task;

import java.io.IOException;
import wt.part.WTPart;

import ext.generic.erp.attributes.AttributeUtil;
import ext.generic.erp.config.ERPPropertiesUtil;
import ext.generic.erp.util.IntegrationConstant;
import ext.generic.erp.util.PDMIntegrationLogUtil;

import com.infoengine.SAK.Task;
import com.infoengine.util.IEException;

public class BomStructureRemoveByParentIETask {
	private static String clazz = BomStructureRemoveByParentIETask.class.getName() ;

	private static boolean verbose = PDMIntegrationLogUtil.verbose ;
	
	private static String ieXMLFileDelete = null ;
	
	static {
		ieXMLFileDelete = (String) ERPPropertiesUtil.getInstance().readProperties().get(IntegrationConstant.BOM_STRUCTURE_REMOVE_BY_PARENT_IE_XML_FILE);
		
		if(ieXMLFileDelete != null){
			ieXMLFileDelete = ieXMLFileDelete.trim() ;
			
			PDMIntegrationLogUtil.printLog(clazz, "Delete Config File : " + ieXMLFileDelete , IntegrationConstant.LOG_LEVEL_NORMAL ) ;
		}else{
			PDMIntegrationLogUtil.printLog(clazz, "Delete Config File : NULL" + ieXMLFileDelete , IntegrationConstant.LOG_LEVEL_ERROR ) ;
		}
	}
	
	/**
	 * 从中间表中删除BOM结构关系的信息
	 * 
	 * @param usageLink
	 */
	public static void delete( WTPart part ){
		PDMIntegrationLogUtil.printLog(clazz, "Entering In delete( WTPart part )......") ;
		
		if( part == null ){
			PDMIntegrationLogUtil.printLog(clazz, "delete Method,WTPart is NULL", IntegrationConstant.LOG_LEVEL_WARN);
			return ;
		}
		
		//创建任务
		Task task = new Task(ieXMLFileDelete);
		
		IETaskCommonUtil.addWhereForRemoveRecordByParent(part, task);
		
		if(verbose){			
			PDMIntegrationLogUtil.printLog(clazz, IETaskCommonUtil.getTaskParams(task), IntegrationConstant.LOG_LEVEL_INFO );
		}
		
		try {
			PDMIntegrationLogUtil.printLog(clazz, "执行BOM结构关系信息删除操作") ;
			
			task.invoke();
			
			PDMIntegrationLogUtil.printLog(clazz, "删除BOM结构关系信息任务结束") ;
		} catch (IEException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IEException", IntegrationConstant.LOG_LEVEL_ERROR);
			PDMIntegrationLogUtil.printLog(clazz, e.toString() , IntegrationConstant.LOG_LEVEL_ERROR);
			
			e.printStackTrace();
		} catch (IOException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IOException", IntegrationConstant.LOG_LEVEL_ERROR);
			PDMIntegrationLogUtil.printLog(clazz, e.getMessage(), IntegrationConstant.LOG_LEVEL_ERROR);
			
			e.printStackTrace();
		}finally{
			PDMIntegrationLogUtil.printLog(clazz, "Existed Out delete( WTPart part )......") ;
		}
	}
}
